package com.quick.springbootquicky.controller;

import com.quick.springbootquicky.page.PageBean;
import com.quick.springbootquicky.pojo.Article;
import com.quick.springbootquicky.service.ArticleService;
import com.quick.springbootquicky.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param article @Validated用于校验参数是否为空，需要同时在实体类中字段属性添加对应的注解
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success("添加成功");
    }

    /**
     * 文章 分页查询
     * @param pageSize 每页条数
     * @param pageNum 第几页
     * @param categoryId 文章分类id
     * @param state 状态
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageBean<Article>> pageList(Integer pageSize,Integer pageNum,
                                              @RequestParam(required = false) Integer categoryId,
                                              @RequestParam(required = false) String state){
        PageBean<Article> pageList = articleService.pageList(pageSize,pageNum,categoryId,state);
        return Result.success("查询成功",pageList);

    }

    /**
     * 搜索
     * @param pageSize
     * @param pageNum
     * @param categoryName
     * @param state
     * @return
     */
    @GetMapping("/search")
    public Result<PageBean<Article>> search(Integer pageSize,Integer pageNum,
                                              @RequestParam(required = false) String categoryName,
                                              @RequestParam(required = false) String state){
        PageBean<Article> pageList = articleService.search(pageSize,pageNum,categoryName,state);
        return Result.success("查询成功",pageList);

    }

    /**
     *  更新文章
     * @param article @Validated(Article.Update.class) Article article 更新时校验参数id，其他的参数在其它方法都要校验
     * @return
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody @Validated(Article.Update.class) Article article){
        articleService.update(article);
        return Result.success("更新成功");
    }

    /**
     * 更新状态 @Validated(Article.Update.class) Integer id 更新时校验参数id，其他的参数在其它方法都要校验
     * @param id
     * @param state
     * @return
     */
    @PutMapping("/updateState")
    public Result<String> updateState(@Validated(Article.Update.class) Integer id,@Validated(Article.Update.class)String state){
        articleService.updateState(id,state);
        return Result.success("更新成功");
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result<String> delete(Integer id){
        articleService.delete(id);
        return Result.success("删除成功");
    }
}
